﻿using System;
using ArtMath.Resource;
using ArtMath.Component.Point;
using ArtMath.Data;
using ArtMath.Attribute;
using ArtMath.Core.Data;
using ArtMath.Core.Helper;

namespace ArtMath.Component.Circle
{
    /// <summary>
    /// 约束圆
    /// </summary>
    [Serializable]
    public class ConstrainedCircle : FullCircleBase
    {
        PadStringX radius;
        public ConstrainedCircle(SketchPad pad, string name, string r, bool addToList = true) : base(pad, name, ToolTypeName.yxbjy, addToList)
        {
            InPoints = new PointBase[1];
            StrRadius = new PadStringX(Pad, r);
            Radius = StrRadius.Value;
        }

        [MultilingualCategory("Structure"), MultilingualDisplayName("CircleCenter")]
        public PointBase CenterPoint
        {
            get { return InPoints[0]; }
            set { SetPoint(0, value); }
        }
        [MultilingualCategory("Structure"), MultilingualDisplayName("Radius")]
        public PadStringX StrRadius
        {
            get { return radius; }
            set
            {
                if (radius is object)
                {
                    radius.ParametersValueChanging -= Radius_ParametersValueChanging;
                    radius.ParametersValueChanged -= Radius_ParametersValueChanged;
                    radius.ExpressionSetting -= Radius_ExpressionSetting;
                    radius.ExpressionSet -= Radius_ExpressionSet;
                }
                radius = value;
                if (radius is object)
                {
                    radius.ParametersValueChanging += Radius_ParametersValueChanging;
                    radius.ParametersValueChanged += Radius_ParametersValueChanged;
                    radius.ExpressionSetting += Radius_ExpressionSetting;
                    radius.ExpressionSet += Radius_ExpressionSet;
                }
            }
        }
        public override double NewRadius
        {
            get
            {
                return StrRadius.Value;
            }
        }
        void Radius_ParametersValueChanging(DocStringX sender)
        {
            CenterPoint.TempMoveLock = true;
            Pad.MoveObject_DoMouseDown(this);
            CenterPoint.TempMoveLock = false;
        }

        void Radius_ExpressionSetting(StringX sender)
        {
            CenterPoint.TempMoveLock = true;
            Pad.MoveObject_DoMouseDown(this);
            CenterPoint.TempMoveLock = false;
        }

        void Radius_ParametersValueChanged(DocStringX sender, ParametersValueChangedEventArgs e)
        {
            if (StrRadius.Value >= 0)
            {
                CenterPoint.TempMoveLock = true;
                if (Pad.MoveObject_DoMouseMove(this, PointD.Empty))
                {
                    ComponentChanged(this);
                }
                else
                {

                }
            }
            Pad.MoveObject_DoMouseUp(this);
            CenterPoint.TempMoveLock = false;
        }
        void Radius_ExpressionSet(StringX sender, ExpressionSetEventArgs e)
        {
            if (StrRadius.Value < 0)
            {
                Pad.ShowMessage(Resources.JoinLocalizedString("Radius", "MustNonNegative"), MessageType.Error);
                StrRadius.SetExpression(e.OldExpression, false);
            }
            else
            {
                CenterPoint.TempMoveLock = true;
                if (Pad.MoveObject_DoMouseMove(this, PointD.Empty))
                {
                    ComponentChanged(this);
                }
                else
                {
                    Pad.ShowMessage(string.Format(Resources.Translate("FailModify"), OfTranslator("Radius")), MessageType.Error);
                    StrRadius.SetExpression(e.OldExpression, false);
                }
            }
            Pad.MoveObject_DoMouseUp(this);
            CenterPoint.TempMoveLock = false;
        }
        public override void SetPoint(int index, PointBase value)
        {
            base.SetPoint(index, value);
            if (value != null)
            {
                switch (index)
                {
                    case 0:
                        ske.Center = value.ToPointD();
                        pske.Center = value.Pxy;
                        break;
                }
            }
        }
        public override PointD NewPCenter
        {
            get
            {
                return CenterPoint.NewPxy;
            }
        }
        public override PointD NewCenter
        {
            get { return CenterPoint.NewXY; }
        }
        public override void CreateComponents()
        {
            if (CenterPoint == null)
            {
                CenterPoint = Pad.CreatePoint(Resources.Translate("CircleCenter"), SpareCoordinate_);
                SpareCoordinate_ = CenterPoint.Pxy;
                Pad.TrackObj = this;
                base.CreateComponents();
            }
        }
        public override object CopyAndPaste(SketchPad pad)
        {
            ConstrainedCircle cc = new ConstrainedCircle(pad, Utility.GetNextAvailableName(Name, pad.CheckObjectNameExist), StrRadius.DirectExpression, LayerItem != null);
            afterNewCopy(cc);
            return cc;
        }
    }
}
